package com.kbbywyclient.ui.helper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.kbbywyclient.R;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.pop.SharePopWin;
import com.kbbywyclient.util.LogUtil;
import com.kbbywyclient.util.ToastUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Jiangzx on 2017/8/19 13:36.
 * 分享帮助类
 */

public class ShareHelper {
    private Activity activity;
    private String from;//分享来源页面，用于谷歌统计

    ProgressDialog dialog;
    private void showLoading(){
        if(dialog==null){
            dialog=ProgressDialog.show(activity,null,activity.getString(R.string.text_wait_a_moment));
        }
        if(!activity.isFinishing()&&!dialog.isShowing()){
            dialog.show();
        }
    }

    private void dismissLoading(){
        if(dialog!=null&&!activity.isFinishing()&&dialog.isShowing()){
            dialog.dismiss();
        }
    }

    public static final class ShareBody {
        public String title="";
        public String text="缤纷权益、优惠好礼，精彩生活，由你掌握";
        public String url="";
        public String pic="";//网络图片路径
        public String picAssetPath="share_pic.jpg";//分享图片asset路径
        public String statisticsTitle="";
        public int platForm=SharePopWin.PLATFORM_WX;//默认分享到微信

        public ShareBody(){}

        @Override
        public String toString() {
            return "title:"+title+
                    "\ntext:"+text+
                    "\nurl:"+url+
                    "\npic:"+pic+
                    "\npicAssetPath:"+picAssetPath+
                    "\nplatForm:"+platForm+
                    "\nstatisticsTitle:"+statisticsTitle;
        }
    }

    public ShareHelper(@NonNull Activity activity,String from){
        this.activity=activity;
        this.from=from;
    }

    public void setFrom(String from){
        this.from=from;
    }

    public  void share(@NonNull ShareBody shareBody){
        LogUtil.d("分享信息:"+ shareBody.toString());
        SHARE_MEDIA share_media = null;
        String platform="";
        switch (shareBody.platForm) {
            case SharePopWin.PLATFORM_WX:
                share_media = SHARE_MEDIA.WEIXIN;
                platform="微信";
                break;
            case SharePopWin.PLATFORM_WX_CIRCLE:
                share_media = SHARE_MEDIA.WEIXIN_CIRCLE;
                platform="微信朋友圈";
                break;
            case SharePopWin.PLATFORM_SINA:
                share_media = SHARE_MEDIA.SINA;
                platform="新浪微博";
                break;
            case SharePopWin.PLATFORM_QQ:
                share_media=SHARE_MEDIA.QQ;
                platform="QQ";
                break;
        }
        if (share_media != null) {
            UMWeb web = new UMWeb(shareBody.url);
            web.setTitle(shareBody.title);
            web.setDescription(shareBody.text);
            if(!TextUtils.isEmpty(shareBody.pic)||!TextUtils.isEmpty(shareBody.picAssetPath)){
                //有分享图片
                UMImage umImage=null;
                if(!TextUtils.isEmpty(shareBody.picAssetPath)){
                    InputStream inputStream=null;
                    try{
                        inputStream=activity.getAssets().open(shareBody.picAssetPath);
                        umImage=new UMImage(activity,BitmapFactory.decodeStream(inputStream));
                    }catch (IOException e){
                        e.printStackTrace();
                    }finally {
                        if(inputStream!=null){
                            try {
                                inputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                //如果设置了网络图片和asset图片，网络图片优先
                if(!TextUtils.isEmpty(shareBody.pic)){
                    umImage=  new UMImage(activity, shareBody.pic);
                }
                if(umImage!=null){
                    web.setThumb(umImage);  //缩略图
                }
            }
            ShareAction shareAction = new ShareAction(activity)
                    .setPlatform(share_media)
                    .setCallback(umShareListener)
                    .withMedia(web);
            if(activity instanceof BaseActivity){
                ((BaseActivity)activity).setScreenAction("分享",from+"-"+platform,
                        shareBody.statisticsTitle);
            }
            shareAction.share();
        }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            showLoading();
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            dismissLoading();
            LogUtil.d("platform:" + platform);
            ToastUtil.showShort("分享成功啦");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            dismissLoading();
            if (t != null) {
                LogUtil.d("throw:" + t.getMessage());
            }
            ToastUtil.showShort("分享失败啦");
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            dismissLoading();
            LogUtil.d("platform:" + platform);
            ToastUtil.showShort("分享取消");
        }
    };

    /**
     * 注意：在activity onActivityResult回调中手动调用此方法
     * @param requestCode requestCode
     * @param resultCode resultCode
     * @param data data
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        dismissLoading();
        UMShareAPI.get(activity).onActivityResult(requestCode,resultCode,data);
    }
}
